﻿using System;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Collections.Generic;

namespace WebUI.BL.Coupon
{
    public class CouponSerialsBL
    {
        protected void clsCouponSerialInsert(tbl_CouponSerial couponserails)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_CouponSerial couponserial = new tbl_CouponSerial();
                    couponserial.CouponSerialsName = couponserails.CouponSerialsName;
                    couponserial.CouponSerialsCode = couponserails.CouponSerialsCode;
                    couponserial.CouponDesc = couponserails.CouponDesc;
                    couponserial.PriceValue = couponserails.PriceValue;
                    couponserial.PercentValue = couponserails.PercentValue;
                    couponserial.MinimumUse = couponserails.MinimumUse;
                    couponserial.ProductCategoryNo = couponserails.ProductCategoryNo;
                    couponserial.ProductCategoryName = couponserails.ProductCategoryName;
                    couponserial.ProductCode = couponserails.ProductCode;
                    couponserial.ProductName = couponserails.ProductName;
                    couponserial.FromDate = couponserails.FromDate;
                    couponserial.ToDate = couponserails.ToDate;
                    couponserial.CreatedUser = couponserails.CreatedUser;
                    couponserial.CreatedDate = DateTime.Now;
                    couponserial.Status = couponserails.Status;

                    adlerContextDb.tbl_CouponSerials.InsertOnSubmit(couponserial);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsCouponSerialModify(tbl_CouponSerial couponserails)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var couponserial = adlerContextDb.tbl_CouponSerials.Single(d => d.CouponSerialsNo == couponserails.CouponSerialsNo);
                    couponserial.CouponSerialsName = couponserails.CouponSerialsName;
                    couponserial.CouponSerialsCode = couponserails.CouponSerialsCode;
                    couponserial.CouponDesc = couponserails.CouponDesc;
                    couponserial.PriceValue = couponserails.PriceValue;
                    couponserial.PercentValue = couponserails.PercentValue;
                    couponserial.MinimumUse = couponserails.MinimumUse;
                    couponserial.ProductCategoryNo = couponserails.ProductCategoryNo;
                    couponserial.ProductCategoryName = couponserails.ProductCategoryName;
                    couponserial.ProductCode = couponserails.ProductCode;
                    couponserial.ProductName = couponserails.ProductName;
                    couponserial.FromDate = couponserails.FromDate;
                    couponserial.ToDate = couponserails.ToDate;
                    couponserial.Status = couponserails.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsCouponSerialDelete(tbl_CouponSerial couponserails)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var couponserial = adlerContextDb.tbl_CouponSerials.Single(d => d.CouponSerialsNo == couponserails.CouponSerialsNo);
                    couponserial.Status = couponserails.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        // Show Coupon Serials.
        public List<MODEL_SP_ADMIN_SEARCH_COUPONSERIALS> Get_Admin_CouponSerials(string couponserialsname, string couponserialscode, string productcategoryno, 
                    string productcode, string fromdate, string todate, string status)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                int index = 1;
                //var query = from n in contextdb.SP_ADMIN_SEARCH_COUPONSERIALS(couponserialsname, couponserialscode, productcategoryno,
                //                    productcode, fromdate, todate, status)
                //            select new MODEL_SP_ADMIN_SEARCH_COUPONSERIALS
                //            {
                //                Index = index++,
                //                CouponSerialsNo = n.CouponSerialsNo,
                //                CouponSerialsName = n.CouponSerialsName,
                //                CouponSerialsCode = n.CouponSerialsCode,
                //                CouponDesc = n.CouponDesc,
                //                PriceValue = n.PriceValue,
                //                PercentValue = n.PercentValue,
                //                MinimumUse = n.MinimumUse,
                //                ProductCategoryNo = n.ProductCategoryNo,
                //                ProductCategoryName = n.ProductCategoryName,
                //                ProductCode = n.ProductCode,
                //                ProductName = n.ProductName,
                //                FromDate = n.FromDate,
                //                ToDate = n.ToDate,
                //                CreatedUser = n.CreatedUser,
                //                CreatedDate = n.CreatedDate,
                //                Dayperiod = n.Dayperiod,
                //                Status = n.Status
                //            };
                //return query.ToList();
                return null;
            }
        }

        public tbl_CouponSerial GetCouponSerialByCouponno(int couponserialsno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                return contextdb.tbl_CouponSerials.Single(d => d.CouponSerialsNo == couponserialsno);
            }
        }
    }
}
